from flask_wtf import FlaskForm
from wtforms import SubmitField, PasswordField, StringField
from wtforms.validators import EqualTo, Length, DataRequired, ValidationError, Regexp
from flask_login import current_user


class InfoForm(FlaskForm):
    name = StringField(
        '姓名',
        validators=[
            DataRequired(message='必填项')
        ]
    )
    identity_card = StringField(
        '身份证',
        validators=[
            Length(15, 18, message='身份证长度不合法')
        ]
    )
    phone_number = StringField(
        '手机号',
        validators=[
            Regexp('^1[345678]\d{9}$', message='不是有效的手机号')
        ]
    )
    save = SubmitField('保存')


class PasswordForm(FlaskForm):
    password_old = PasswordField(
        '当前密码',
        validators=[
            DataRequired(message='必填项')
        ]
    )
    password = PasswordField(
        '新密码',
        validators=[
            DataRequired(message='必填项'),
            Length(8, 128, message='新设密码长度8-128个字符之间')
        ]
    )
    password_repeat = PasswordField(
        '重复密码',
        validators=[
            EqualTo('password', message='重复密码与新密码不相同')
        ]
    )
    save = SubmitField('保存')

    def validate_password_old(self, field):
        if not current_user.check_password(self.password_old.data):
            raise ValidationError('当前密码不正确')

